import sys
import os
import cv2
import shutil
import numpy as np
from mtcnn_tf.mtcnn_batchprocess import mtcnn_batchprocess

c_mtcnn = mtcnn_batchprocess()
path_list1, file_list1 = c_mtcnn.get_file_list(file_path = '/ai/dataset/age_gender/20180115/clear2000', file_type = '.png')
# print len(path_list1)
# c_mtcnn.mtcnn_process(path_list = path_list1, file_list = file_list1, 
# 	margin_ratio = 0.143,resize_type = 2, controled_size = 256, error_pass = True, error_moveFile = True) 
move_path = '/ai/dataset/age_gender/20180115/clear2000_profile123'
image_num = len(path_list1)
print image_num
for cur_index in xrange(image_num):
	c_path = path_list1[cur_index]
	c_file = file_list1[cur_index]
	src_file = os.path.join(c_path, c_file)
	# src_file = '/ai/oeasy_work/mtcnn_1124_1206/mtcnn_tf_caffe/mtcnn_tf/9d27703c4580ecfd964830c7dc4a09c0.jpg'
	try:
		draw = cv2.imread(src_file)
		img_shape = draw.shape
		img_width = img_shape[1]
		img_height = img_shape[0]
		# print img_width	,img_height     
		# exit()
		bounding_boxes, points = c_mtcnn.m_mtcnn.detect_face(draw, intput_type = 1)
		if type(bounding_boxes) is np.ndarray:
			nrof_faces = bounding_boxes.shape[0]
			print('Total %d face(s) detected' % (nrof_faces))
			for i in xrange(nrof_faces):
				c_box    = bounding_boxes[i]
				c_points = points[:,i]
				if c_mtcnn.m_mtcnn.face_is_normal(c_box, c_points, img_width, img_height):
					print "normal"
				else:
					print "profile"
					cv2.rectangle(draw, (c_box[0],c_box[1]), (c_box[2],c_box[3]), (0, 255, 0), 3) 
					for points_index in xrange(5):
						c_point_x = c_points[points_index]
						c_point_y = c_points[points_index + 5]
						cv2.circle(draw, (c_point_x, c_point_y), 5, (0, 0, 255), 4) 
					cv2.imwrite(src_file, draw)
					shutil.move(src_file, move_path)
		else:
			print "no face"
			shutil.move(src_file, move_path)
	except:
		print "err_file : " + src_file
		exit()

